
<template>
  <div>
    <el-card class="Hys_card">
      <!-- 点击添加用户弹出模态框 然后给这个按钮绑定个事件 将这个事件设置为ture  之后在data里定义数据对象
      更改组件里面的内容-->
      <el-button type="success" @click="addlist">添加用户</el-button>
    </el-card>
    <el-table :data="rolesList" border style="width: 100%">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <div class="cont" v-for="(item,index) in props.row.children" :key="index">
              <!-- 左侧 -->
              <div class="left">
                <el-button type="primary">
                  {{item.authName}}
                  <i class="el-icon-close el-icon--right" @click="del(item.id,props.row.id)"></i>
                </el-button>
              </div>
              <!-- 右侧 -->
              <div class="right" v-for="(item1,index) in item.children" :key="index">
                <div class="right_one">
                  <el-button type="success">
                    {{item1.authName}}
                    <i class="el-icon-close el-icon--right" @click="del(item.id,props.row.id)"></i>
                  </el-button>
                </div>
                <div class="right_two" v-for="(item2,index) in item1.children" :key="index">
                  <el-button type="warning">
                    {{item2.authName}}
                    <i class="el-icon-close el-icon--right" @click="del(item.id,props.row.id)"></i>
                  </el-button>
                </div>
              </div>
            </div>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="商品 ID" prop="id"></el-table-column>
      <el-table-column label="角色名称" prop="roleName"></el-table-column>
      <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
      <el-table-column label="操作" width="300">
        <template v-slot="scope">
          <!-- 修改按钮 -->
          <el-button size="mini" type="primary" icon="el-icon-edit" @click="compile(scope.row)">编辑</el-button>
          <!-- 删除按钮 -->
          <el-button
            type="danger"
            icon="el-icon-delete-solid"
            size="mini"
            @click="deleteIDs(scope.row)"
          >删除</el-button>
          <!-- 设置按钮 -->
          <el-button size="mini" type="warning" icon="el-icon-s-tools" @click="show(scope.row)">分配权限</el-button>
        </template>
      </el-table-column>
      <!-- 模态框 -->
      <!-- 对话框 -->
    </el-table>
    <el-dialog title="提示" :visible.sync="dialogVisible" width="50%">
      <!-- 内容主体区 -->
      <el-form :model="Input" :rules="rules" ref="ruleForm" label-width="100px">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="Input.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="Input.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <!-- 内容按钮区 -->

      <span slot="footer" class="dialog-footer">
        <el-button @click="set">取 消</el-button>
        <el-button type="primary" @click="end">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑的对话框 -->
    <el-dialog title="提示" :visible.sync="dialogVisibles" width="50%">
      <el-form :model="Inputs" :rules="rules" ref="ruleForm" label-width="100px">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="Inputs.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="Inputs.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibles=false">取 消</el-button>
        <el-button type="primary" @click="modification">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 分配权限对话款 -->
    <el-dialog title="提示" :visible.sync="dialogVisibless" width="30%">
      <el-tree
        :data="data"
        show-checkbox
        node-key="id"
        :default-expanded-keys="[2, 3]"
        :default-checked-keys="[5]"
        :props="defaultProps"
        ref="tree"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibless = false">取 消</el-button>
        <el-button type="primary" @click="queding">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      rolesList: [],
      //分配权限的数组
      data: [],
      Input: {
        roleName: "",
        roleDesc: ""
      },
      Inputs: {
        roleName: 1,
        roleDesc: 2
      },
      rules: {
        roleName: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        roleDesc: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ]
      },
      dialogVisible: false,
      dialogVisibles: false,
      dialogVisibless: false,
      defaultProps: {
        children: "children",
        label: "authName"
      },
      id: "",
      suib: []
    };
  },
  created() {
    this.addForm();
  },
  methods: {
    async addForm() {
      const res = await this.$http.get("/roles");
      let { data, meta } = res.data;
      if (meta.status == 200) {
        console.log(meta);
        this.rolesList = data;
      }
    },
    addlist() {
      this.dialogVisible = true;
    },
    set() {
      this.$refs.ruleForm.resetFields();
      this.dialogVisible = false;
    },
    async end() {
      const res = await this.$http.post("roles", this.rolesFrom);
      let { meta } = res.data;
      if (meta.status == 201) {
        this.$message.success("创建成功");
        this.Input = {};
        this.addForm();
        this.dialogVisible = false;
      }
    },
    async compile(scope) {
      this.dialogVisibles = true;
      const res = await this.$http.delete(`roles/${role.id}/rights/${rightId}`);
      let { data, meta } = res.data;
      if (meta.status == 200) {
        this.Inputs = data;
      }
    },
    async modification() {
      const res = await this.$apis.modificationID(this.Inputs);
      let { meta } = res.data;
      if (meta.status == 200) {
        this.$message.success("修改成功");
        this.addForm();
        this.dialogVisibles = false;
      }
    },
    async deleteIDs(scope) {
      const res = await this.$http.get("rights/tree");
      let { meta } = res.data;
      if (meta.status == 200) {
        try {
          await this.$ask("你确定要删除嘛？");
        } catch (error) {
          console.log(error);
        }
        this.addForm();
      }
    },
    async show(scope) {
      this.dialogVisibless = true;
      //这里的ID赋值到上面的ID里
      this.id = scope.id;
      const res = await this.$apis.showID();
      let { data, meta } = res.data;
      if (meta.status == 200) {
        this.data = data;
      }
    },
    async queding() {
      //获取自己的数组父级数据id
      var aa = this.$refs.tree.getHalfCheckedKeys();
      //获取子级数据id  然后放到数组里
      this.suib = this.$refs.tree.getCheckedKeys();
      //把父级的数据ID加入到数组 合并到一起
      this.suib.push(...aa);
      //把数组的数据转换字符串
      var bb = this.suib.toString();
      //这里的id是上面data里面定义的  是上面赋值到data数组里的
      const res = await this.$apis.quedingID(this.id, bb);
      let { meta } = res.data;
      if (meta.status == 200) {
        this.$message.success("更新成功");
        this.dialogVisibless = false;
        this.addForm();
      }
    },
    async del(id, ids) {
      console.log(id, ids);
      const res = await this.$apis.delId({
        ids,
        id
      });
      let { meta } = res.data;
      if (meta.status == 200) {
        this.$ask("确定删除嘛");
        this.addForm();
      }
    }
  }
};
</script>
<style lang="scss">
.Hys_card {
  margin-top: 10px;
}
.cont {
  width: 100%;
  display: flex;
  // height: 500px;
  .left {
    width: 30%;
    // height: 500px;
  }
  .right {
    // flex: 1;
    width: 70%;
    display: flex;
    .right_one {
      width: 300px;
    }
    .right_two {
      flex: 1;
    }
  }
}
</style>
